package cn.edu.sgu.www.mhxysy.service.jingmai.impl;

import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.jingmai.JingmaiCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.jingmai.JingmaiCategory;
import cn.edu.sgu.www.mhxysy.mapper.jingmai.JingmaiCategoryMapper;
import cn.edu.sgu.www.mhxysy.pager.jingmai.JingmaiCategoryPager;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.jingmai.JingmaiCategoryService;
import cn.edu.sgu.www.mhxysy.util.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class JingmaiCategoryServiceImpl implements JingmaiCategoryService {

    private final UploadUtils uploadUtils;
    private final ImageUpdater imageUpdater;
    private final JingmaiCategoryMapper jingmaiCategoryMapper;

    @Autowired
    public JingmaiCategoryServiceImpl(
            UploadUtils uploadUtils,
            ImageUpdater imageUpdater,
            JingmaiCategoryMapper jingmaiCategoryMapper) {
        this.uploadUtils = uploadUtils;
        this.imageUpdater = imageUpdater;
        this.jingmaiCategoryMapper = jingmaiCategoryMapper;
    }

    @Override
    public String upload(MultipartFile file) throws IOException {
        String directory = CustomDirectories.DIRECTORY_JINGMAI;

        return uploadUtils.upload(file, directory);
    }

    @Override
    public void insert(JingmaiCategoryDTO jingmaiCategoryDTO) {
        JingmaiCategory jingmaiCategory = toEntity(jingmaiCategoryDTO);

        jingmaiCategory.setId(null);

        jingmaiCategoryMapper.insert(jingmaiCategory);
    }

    @Override
    public void deleteById(Integer id) {
        jingmaiCategoryMapper.deleteById(id);
    }

    @Override
    public void updateById(JingmaiCategoryDTO jingmaiCategoryDTO) {
        ObjectUtils.requireNonNull(jingmaiCategoryDTO.getId(), CustomMessages.ID_NOT_NULL);

        JingmaiCategory jingmaiCategory = toEntity(jingmaiCategoryDTO);

        jingmaiCategoryMapper.updateById(jingmaiCategory);
    }

    @Override
    public void updateImageById(Integer id, String image) {
        imageUpdater.updateImageById(image, new Consumer<Object>() {
            @Override
            public void accept(Object o) {
                UpdateWrapper<JingmaiCategory> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

                jingmaiCategoryMapper.update(updateWrapper.getEntity(), updateWrapper);
            }
        });
    }

    @Override
    public JingmaiCategory selectById(Integer id) {
        return jingmaiCategoryMapper.selectById(id);
    }

    @Override
    public List<JingmaiCategory> selectMaster(Integer schoolId) {
        QueryWrapper<JingmaiCategory> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("school_id", schoolId);

        queryWrapper.nested(new Consumer<QueryWrapper<JingmaiCategory>>() {
            @Override
            public void accept(QueryWrapper<JingmaiCategory> queryWrapper) {
                queryWrapper.gt("type", 2);

                queryWrapper.or();

                queryWrapper.nested(new Consumer<QueryWrapper<JingmaiCategory>>() {
                    @Override
                    public void accept(QueryWrapper<JingmaiCategory> queryWrapper) {
                        queryWrapper.eq("type", 1);
                        queryWrapper.eq("seq", 7);
                    }
                });
            }
        });

        queryWrapper.orderByAsc("id");

        return jingmaiCategoryMapper.selectList(queryWrapper);
    }

    @Override
    public List<JingmaiCategory> selectBySchoolId(Integer schoolId) {
        QueryWrapper<JingmaiCategory> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(
                schoolId != null,
                "school_id", schoolId
        );

        return jingmaiCategoryMapper.selectList(queryWrapper);
    }

    @Override
    public Page<JingmaiCategory> selectByPage(JingmaiCategoryPager pager) {
        QueryWrapper<JingmaiCategory> queryWrapper = new QueryWrapper<>();
        Page<JingmaiCategory> page = Pager.ofPage(pager);

        queryWrapper.like(
                StringUtils.isNotEmpty(pager.getName()),
                "name", pager.getName()
        );
        queryWrapper.eq(
                pager.getType() != null,
                "type", pager.getType()
        );
        queryWrapper.eq(
                pager.getSchoolId() != null,
                "school_id", pager.getSchoolId()
        );

        return jingmaiCategoryMapper.selectPage(page, queryWrapper);
    }

    private JingmaiCategory toEntity(JingmaiCategoryDTO jingmaiCategoryDTO) {
        Integer id = jingmaiCategoryDTO.getId();
        String name = jingmaiCategoryDTO.getName();

        // 验证名称重复
        QueryWrapper<JingmaiCategory> queryWrapper = new QueryWrapper<>();

        queryWrapper.ne(id != null, "id", id);
        queryWrapper.eq("name", name);

        Long count = jingmaiCategoryMapper.selectCount(queryWrapper);

        if (count > 0) {
            throw new GlobalException(ResponseCode.CONFLICT, "经脉名称重复！");
        }

        // 封装结果对象
        JingmaiCategory jingmaiCategory = new JingmaiCategory();

        // 必填字段
        jingmaiCategory.setId(id);
        jingmaiCategory.setName(name);
        jingmaiCategory.setSeq(jingmaiCategoryDTO.getSeq());
        jingmaiCategory.setType(jingmaiCategoryDTO.getType());
        jingmaiCategory.setSchoolId(jingmaiCategoryDTO.getSchoolId());
        jingmaiCategory.setDirection(jingmaiCategoryDTO.getDirection());
        jingmaiCategory.setActiveEffect(jingmaiCategoryDTO.getActiveEffect());
        // 非必填字段
        jingmaiCategory.setPrevId(jingmaiCategoryDTO.getPrevId());
        jingmaiCategory.setNextId(jingmaiCategoryDTO.getNextId());
        jingmaiCategory.setInlayEffect(jingmaiCategoryDTO.getInlayEffect());

        return jingmaiCategory;
    }

}